﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Snom
{
    class EventsRegistration
    {
        private IPAddress _localIpAddress;
        private int _listeningPort;
        private Browser _browser;

        public EventsRegistration(string phoneIpAddress, int listeningPort)
            : this(phoneIpAddress, null, null, listeningPort)
        {
        }

        public EventsRegistration(string phoneIpAddress, string username, string password, int listeningPort)
        {
            PhoneIpAddress = phoneIpAddress;
            _localIpAddress = NetUtil.GetLocalIpAddress();
            _listeningPort = listeningPort;
            _browser = new Browser();
        }

        public string Username
        {
            get;
            set;
        }

        public string Password
        {
            get;
            set;
        }

        public string PhoneIpAddress
        {
            get;
            set;
        }

        public bool UseCredentials
        {
            get
            {
                return !string.IsNullOrEmpty(Username) || !string.IsNullOrEmpty(Password);
            }
        }

        public void Register()
        {
            if (UseCredentials)
            {
                Login();
            }

            string content = "action_dnd_on_url=" + GetActionUrl(SnomEventType.DndOn) +
                             "&action_dnd_off_url=" + GetActionUrl(SnomEventType.DndOff) +
                             "&action_redirection_on_url=" + GetActionUrl(SnomEventType.CallForwardingOn) +
                             "&action_redirection_off_url=" + GetActionUrl(SnomEventType.CallForwardingOff) +
                             "&action_incoming_url=" + GetActionUrl(SnomEventType.IncomingCall) +
                             "&action_outgoing_url=" + GetActionUrl(SnomEventType.OutgoingCall) +
                             "&action_setup_url=" + GetActionUrl(SnomEventType.SetupFinished) +
                             "&action_offhook_url=" + GetActionUrl(SnomEventType.Offhook) +
                             "&action_onhook_url=" + GetActionUrl(SnomEventType.Onhook) +
                             "&action_missed_url=" + GetActionUrl(SnomEventType.MissedCall) +
                             "&action_reg_failed=" + GetActionUrl(SnomEventType.RegistrationFailed) +
                             "&action_connected_url=" + GetActionUrl(SnomEventType.Connected) +
                             "&action_disconnected_url=" + GetActionUrl(SnomEventType.Disconnected) +
                             "&action_log_on_url=" + GetActionUrl(SnomEventType.Logon) +
                             "&action_log_off_url=" + GetActionUrl(SnomEventType.LogOff) +
                             "&action_hold=" + GetActionUrl(SnomEventType.HoldCall) +
                             "&action_unhold=" + GetActionUrl(SnomEventType.UnholdCall) +
                             "&action_transfer=" + GetActionUrl(SnomEventType.TransferCall) +
                             "&action_blind_transfer=" + GetActionUrl(SnomEventType.BlindTransfer) +
                             "&action_attended_transfer=" + GetActionUrl(SnomEventType.AttendedTransfer) +
                             "&action_received_attended_transfer=" + GetActionUrl(SnomEventType.ReceivedAttendedTransfer) +
                             "&Settings=Save";

            SetActionUrlsHttpRequest(content);
        }

        private void SetActionUrlsHttpRequest(string content)
        {
            string actionsPageUrl = string.Format("http://{0}/action.htm", PhoneIpAddress);
            _browser.GetHttpPostStream(actionsPageUrl, actionsPageUrl, "application/x-www-form-urlencoded", content);
        }

        private string GetActionUrl(SnomEventType snomEventType)
        {
            return Uri.EscapeDataString(string.Format(
                "http://{0}:{1}/Events?event_type={2}&local=$local&remote=$remote&active_url=$active_url&active_user=$active_user&active_host=$active_host&csta_id=$csta_id&call-id=$call-id&display_local=$display_local&display_remote=$display_remote&expansion_module=$expansion_module&$active_key=$active_key", 
                _localIpAddress,
                _listeningPort,
                snomEventType));
        }

        public void Unregister()
        {
            string content = "action_dnd_on_url=" +
                             "&action_dnd_off_url=" +
                             "&action_redirection_on_url=" +
                             "&action_redirection_off_url=" +
                             "&action_incoming_url=" +
                             "&action_outgoing_url=" +
                             "&action_setup_url=" +
                             "&action_offhook_url=" +
                             "&action_onhook_url=" +
                             "&action_missed_url=" +
                             "&action_reg_failed=" +
                             "&action_connected_url=" +
                             "&action_disconnected_url=" +
                             "&action_log_on_url=" +
                             "&action_log_off_url=" +
                             "&action_hold=" +
                             "&action_unhold=" +
                             "&action_transfer=" +
                             "&action_blind_transfer=" +
                             "&action_attended_transfer=" +
                             "&action_received_attended_transfer=" +
                             "&Settings=Save";

            SetActionUrlsHttpRequest(content);
        }

        //action_dnd_on_url=&action_dnd_off_url=&action_redirection_on_url=&action_redirection_off_url=&action_incoming_url=http%3A%2F%2F10.1.0.250&action_outgoing_url=&action_setup_url=&action_offhook_url=&action_onhook_url=&action_missed_url=&action_reg_failed=&action_connected_url=&action_disconnected_url=&action_log_on_url=&action_log_off_url=&action_hold=&action_unhold=&action_transfer=&action_blind_transfer=&action_attended_transfer=&action_received_attended_transfer=&Settings=Save

        private void Login()
        {
            throw new NotImplementedException();
        }


    }
}
